package com.simple.redissonrpc.handler;

import com.simple.redissonrpc.core.GameRpcMsgContext;
import com.simple.redissonrpc.core.RpcMsgReceiver;
import com.simple.redissonrpc.msg.TestAnswer;
import com.simple.redissonrpc.msg.TestAsk;
import com.simple.redissonrpc.msg.TestMessage;
import java.util.Random;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

/**
 * 其它服务调用游戏服
 */
@Slf4j
@Controller
public class GameRpcHandler {

    @RpcMsgReceiver(TestMessage.class)
    public void testSendMsg(GameRpcMsgContext context) {
        TestMessage testMessage = context.getMsg();

        log.info("客户端发来请求:{}", testMessage.getTag());
    }

    @RpcMsgReceiver(TestAsk.class)
    public void testAsk(GameRpcMsgContext context) {
        TestAsk testAsk = context.getMsg();

        // TODO 测试异常
        if (new Random().nextBoolean()) {
//            throw new RuntimeException("aa");
        }
//        log.info("客户端发来ask请求:{}", testAsk.getTag());

        TestAnswer testAnswer = new TestAnswer();
        testAnswer.setTag(testAsk.getTag() + "response");
        context.sendAnswerMsg(testAnswer);
    }
}
